package com.hqu.environment.task;

import com.hqu.environment.constant.Constant;
import com.hqu.environment.dao.ElementStatusDao;
import com.hqu.environment.dao.EquipmentCategoryDao;
import com.hqu.environment.dao.FarmDao;
import com.hqu.environment.dao.NewsDao;
import com.hqu.environment.dao.TimeValueDao;
import com.hqu.environment.dto.ElementStatusDTO;
import com.hqu.environment.dto.FarmDTO;
import com.hqu.environment.dto.NewsDTO;
import com.hqu.environment.dto.NewsUserDTO;
import com.hqu.environment.dto.TimeValueDTO;
import com.hqu.environment.dto.UserFarmDTO;
import com.hqu.environment.enums.BooleanEnum;
import com.hqu.environment.enums.NewsTypeEnum;
import com.hqu.environment.utils.Util;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * @Description: 获取农场环境定时任务
 * @Date: 2019/05/16 21:14
 * @Author: 邓勇嘉
 **/
@Component
public class TimingTask {

    @Autowired
    FarmDao farmDao;
    @Autowired
    TimeValueDao timeValueDao;
    @Autowired
    EquipmentCategoryDao equipmentCategoryDao;
    @Autowired
    ElementStatusDao elementStatusDao;
    @Autowired
    NewsDao newsDao;
    @Autowired
    SqlSession sqlSession;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void getTimeValue() {
        List<ElementStatusDTO> elementStatusList = elementStatusDao.selectList();
        Date now = new Date();

        ElementStatusDTO temp;
        TimeValueDTO timeValueDTO;
        NewsDTO newsDTO;
        NewsDao newsDaoSqlSession=sqlSession.getMapper(NewsDao.class);
        TimeValueDao timeValueDaoSqlSession=sqlSession.getMapper(TimeValueDao.class);

        for(ElementStatusDTO item : elementStatusList){
            Double value = RandomValue(item);
            temp=new ElementStatusDTO();
            temp.setId(item.getId());
            temp.setValue(value);

            if(  value>item.getMonitorMax()||value<item.getMonitorMin()) {
                temp.setStatus(BooleanEnum.YES.getIndex());
            }else{
                temp.setStatus(BooleanEnum.NO.getIndex());
            }
            elementStatusDao.updateByPrimaryKeySelective(temp);

            newsDTO=new NewsDTO();
            newsDTO.setFarm(item.getFarm());
            newsDTO.setElement(item.getElementName());
            if(value>item.getMonitorMax()||value<item.getMonitorMin()){
                NewsDTO n = newsDao.selectLatestNews(newsDTO);
                if(n==null || n.getType()==NewsTypeEnum.NORMAL.getIndex()){
                    FarmDTO f=farmDao.selectByPrimaryKey(item.getFarm());
                    newsDTO.setTitle(f.getName()+"超标警告");
                    newsDTO.setContent("您所监控的农场"+f.getName()+item.getElementName()+"已经超标，请尽快处理");
                    newsDTO.setType(NewsTypeEnum.WARN.getIndex());
                    newsDaoSqlSession.insertSelective(newsDTO);
                    List<UserFarmDTO> userFarmList = farmDao.selectUserFarmList(item.getFarm());

                    for (UserFarmDTO ufitem: userFarmList){
                        newsDaoSqlSession.insertNewsUser(new NewsUserDTO(newsDTO.getId(),ufitem.getUser()));
                    }
                }

            }else {

                NewsDTO n = newsDao.selectLatestNews(newsDTO);

                if(n!=null&&n.getType()==NewsTypeEnum.WARN.getIndex()){
                    FarmDTO f=farmDao.selectByPrimaryKey(item.getFarm());
                    newsDTO.setTitle(f.getName()+"超标警告解除");
                    newsDTO.setContent("您所监控的农场"+f.getName()+item.getElementName()+"超标情况已经解除");
                    newsDTO.setType(NewsTypeEnum.NORMAL.getIndex());

                    newsDaoSqlSession.insertSelective(newsDTO);

                    List<UserFarmDTO> userFarmList = farmDao.selectUserFarmList(item.getFarm());

                    for (UserFarmDTO ufitem: userFarmList){
                        newsDaoSqlSession.insertNewsUser(new NewsUserDTO(newsDTO.getId(),ufitem.getUser()));
                    }
                }



            }


            if((now.getTime()- Constant.ONE_HOUR)% Constant.THREE_HOUR<Constant.ONE_MINUTE){
                timeValueDTO = new TimeValueDTO();
                timeValueDTO.setFarm(item.getFarm());
                timeValueDTO.setTime(new Date());
                timeValueDTO.setEquipmentCategory(item.getEquipmentCategory());
                timeValueDTO.setValue(value);
                timeValueDaoSqlSession.insertSelective(timeValueDTO);
            }
        }
    }




    private double RandomValue(ElementStatusDTO item){
        Double maxLimit=item.getMaxLimit();
        Double minLimit=item.getMinLimit();

        Double randomFlied = (maxLimit-minLimit)/40;

        double minRandom =item.getSettingValue()-randomFlied;
        double maxRandom =item.getSettingValue()+randomFlied;
        if(minRandom<0){
            minRandom=0;
        }
        if(maxRandom>maxLimit){
            maxRandom=maxLimit;
        }
        Double value = Util.getRandom(minRandom,maxRandom);
        if(maxLimit<200){
           return new BigDecimal(value).setScale(2, RoundingMode.UP).doubleValue();
        }else{
            return Math.floor(value);
        }
    }
}
